package com.wizinno.android.urineflow.serialport;

import cn.hutool.core.util.HexUtil;
import com.aill.androidserialport.ByteUtil;
import com.wizinno.android.urineflow.constant.CommonControl;
import com.wizinno.android.urineflow.util.BytesUtil;

import java.math.BigInteger;
import java.util.List;

/**
 * 给串口发送命令
 *
 * @author LiuMei
 * @date 2020-11-24.
 */
public class SerialPortUtil {

    /**
     * 2字节转换成int
     *
     * @param byte1
     * @param byte2
     * @return
     */
    public static Integer byte2TransToInt(byte byte1, byte byte2) {
        byte[] bytes = new byte[2];
        bytes[0] = byte1;
        bytes[1] = byte2;
        Integer value = new BigInteger(ByteUtil.hexBytesToString(bytes), 16).intValue();
        return value;
    }

    /**
     * int转成2个字节
     *
     * @param i
     * @return
     */
    public static byte[] integerTrans2Byte(Integer i) {
        byte[] result = new byte[2];
        result[0] = (byte) ((i >> 8) & 0xFF);
        result[1] = (byte) (i & 0xFF);
        return result;
    }

    public static float transWeight(byte[] receiveBytes) {
        byte[] weightBytes = new byte[4];
        weightBytes[0] = receiveBytes[2];
        weightBytes[1] = receiveBytes[3];
        weightBytes[2] = receiveBytes[4];
        weightBytes[3] = receiveBytes[5];
        Integer weightMg = new BigInteger(ByteUtil.hexBytesToString(weightBytes), 16).intValue();
        float weight = (float) (weightMg * 1.0 / 1000);
        return weight;
    }

    public static Integer transAdc(byte[] receiveBytes) {
        byte[] weightBytes = new byte[4];
        weightBytes[0] = receiveBytes[2];
        weightBytes[1] = receiveBytes[3];
        weightBytes[2] = receiveBytes[4];
        weightBytes[3] = receiveBytes[5];
        Integer adc = new BigInteger(ByteUtil.hexBytesToString(weightBytes), 16).intValue();
        return adc;
    }

    public static void readWeight() {
        if (CommonControl.INSTANCE.usbHelper != null) {
            CommonControl.INSTANCE.usbHelper.usbSendData(SerialProtocol.READ_WEIGHT);
        }
    }

    public static void readAdc() {
        if (CommonControl.INSTANCE.usbHelper != null) {
            CommonControl.INSTANCE.usbHelper.usbSendData(SerialProtocol.READ_ADC);
            try {
                Thread.sleep(200);
                CommonControl.INSTANCE.usbHelper.usbSendData(SerialProtocol.READ_CHECK_0);
                Thread.sleep(200);
                CommonControl.INSTANCE.usbHelper.usbSendData(SerialProtocol.READ_CHECK_500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void writeCheck0(Integer value) {
        if (CommonControl.INSTANCE.usbHelper != null) {
            String hexStr = HexUtil.toHex(value);
            byte[] statusByte = BytesUtil.append(SerialProtocol.WRITE_CHECK_0_PREFIX, ByteUtil.hexStringToBytes(hexStr));
            CommonControl.INSTANCE.usbHelper.usbSendData(statusByte);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            readAdc();
        }
    }

    public static void writeCheck500(Integer value) {
        if (CommonControl.INSTANCE.usbHelper != null) {
            String hexStr = HexUtil.toHex(value);
            byte[] statusByte = BytesUtil.append(SerialProtocol.WRITE_CHECK_500_PREFIX, ByteUtil.hexStringToBytes(hexStr));
            CommonControl.INSTANCE.usbHelper.usbSendData(statusByte);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            readAdc();
        }
    }

    /**
     * 开始连续称重
     */
    public static void startWeight() {
        if (CommonControl.INSTANCE.usbHelper != null) {
            CommonControl.INSTANCE.usbHelper.usbSendData(SerialProtocol.COLLECT_START);
            CommonControl.INSTANCE.isWight = true;
        }
    }

    /**
     * 结束连续称重
     */
    public static void stopWeight() {
        if (CommonControl.INSTANCE.usbHelper != null) {
            CommonControl.INSTANCE.usbHelper.usbSendData(SerialProtocol.COLLECT_END);
            CommonControl.INSTANCE.isWight = false;
        }
    }

    /**
     * 结束采集后清洗
     */
    public static void wash() {
        if (CommonControl.INSTANCE.usbHelper != null) {
            //todo 清洗
        }
    }

    /**
     * 舵机设置为0度
     */
    public static void steering0() {
        if (CommonControl.INSTANCE.usbHelper != null) {
            CommonControl.INSTANCE.usbHelper.usbSendData(SerialProtocol.TURN0);
            try {
                Thread.sleep(200);
                readSteeringStatus();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 舵机设置90度
     */
    public static void steering90() {
        if (CommonControl.INSTANCE.usbHelper != null) {
            CommonControl.INSTANCE.usbHelper.usbSendData(SerialProtocol.TURN90);
            try {
                Thread.sleep(200);
                readSteeringStatus();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 舵机清洗
     */
    public static void steeringWash() {
        if (CommonControl.INSTANCE.usbHelper != null) {
            CommonControl.INSTANCE.usbHelper.usbSendData(SerialProtocol.WASH);
            try {
                Thread.sleep(200);
                readSteeringStatus();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 读取阀门状态
     */
    public static void readValveStatus() {
        if (CommonControl.INSTANCE.usbHelper != null) {
            CommonControl.INSTANCE.usbHelper.usbSendData(SerialProtocol.VALVE_STATUS_READ_PREFIX);
        }
    }

    /**
     * 写入阀门状态
     */
    public static void writeValveStatus(byte status) {
        if (CommonControl.INSTANCE.usbHelper != null) {
            byte[] statusByte = BytesUtil.append(SerialProtocol.VALVE_STATUS_WRITE_PREFIX, status);
            CommonControl.INSTANCE.usbHelper.usbSendData(statusByte);
        }
    }

    /**
     * 读取距离相关设置
     */
    public static void readDistance() {
        if (CommonControl.INSTANCE.usbHelper != null) {
            CommonControl.INSTANCE.usbHelper.usbSendData(SerialProtocol.DISTANCE_BUTTON);
            try {
                Thread.sleep(200);
                CommonControl.INSTANCE.usbHelper.usbSendData(SerialProtocol.DISTANCE_BUTTON_TIME);
                Thread.sleep(200);
                CommonControl.INSTANCE.usbHelper.usbSendData(SerialProtocol.DISTANCE_PEOPLE);
                Thread.sleep(200);
                CommonControl.INSTANCE.usbHelper.usbSendData(SerialProtocol.DISTANCE_PEOPLE_TIME);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 写入距离设置
     * 写入顺序:
     * 按钮ON距离-按钮OFF距离
     * 人靠近距离-人离开距离
     * 按钮ON抖动时间-按钮OFF抖动时间
     * 人靠近抖动时间-人离开抖动时间
     *
     * @param values
     */
    public static void writeDistance(List<Integer> values) {
        if (CommonControl.INSTANCE.usbHelper != null) {
            byte[] onDistance = integerTrans2Byte(values.get(0));
            byte[] offDistance = integerTrans2Byte(values.get(1));
            byte[] buttonWrite = BytesUtil.append(SerialProtocol.DISTANCE_BUTTON_WRITE_PREFIX, onDistance, offDistance);
            CommonControl.INSTANCE.usbHelper.usbSendData(buttonWrite);
            try {
                Thread.sleep(200);
                byte[] nearDistance = integerTrans2Byte(values.get(2));
                byte[] leaveDistance = integerTrans2Byte(values.get(3));
                byte[] peopleWrite = BytesUtil.append(SerialProtocol.DISTANCE_PEOPLE_WRITE_PREFIX, nearDistance, leaveDistance);
                CommonControl.INSTANCE.usbHelper.usbSendData(peopleWrite);
                Thread.sleep(200);
                byte[] onTime = integerTrans2Byte(values.get(4));
                byte[] offTime = integerTrans2Byte(values.get(5));
                byte[] buttonTimeWrite = BytesUtil.append(SerialProtocol.DISTANCE_BUTTON_TIME_WRITE_PREFIX, onTime, offTime);
                CommonControl.INSTANCE.usbHelper.usbSendData(buttonTimeWrite);
                Thread.sleep(200);
                byte[] nearTime = integerTrans2Byte(values.get(6));
                byte[] leaveTime = integerTrans2Byte(values.get(7));
                byte[] peopleTimeWrite = BytesUtil.append(SerialProtocol.DISTANCE_PEOPLE_TIME_WRITE_PREFIX, nearTime, leaveTime);
                CommonControl.INSTANCE.usbHelper.usbSendData(peopleTimeWrite);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            readDistance();
        }
    }

    /**
     * 读取舵机状态
     */
    public static void readSteeringStatus() {
        if (CommonControl.INSTANCE.usbHelper != null) {
            CommonControl.INSTANCE.usbHelper.usbSendData(SerialProtocol.READ_STEERING_STATUS);
        }
    }

    /**
     * 当前角度、编码器值
     */
    public static void readSteeringAngle(){
        if (CommonControl.INSTANCE.usbHelper != null) {
            CommonControl.INSTANCE.usbHelper.usbSendData(SerialProtocol.READ_STEERING_ANGLE);
        }
    }

    public static void readVersion() {
        if (CommonControl.INSTANCE.usbHelper != null) {
            CommonControl.INSTANCE.usbHelper.usbSendData(SerialProtocol.A1_VERSION);
            try {
                Thread.sleep(200);
                CommonControl.INSTANCE.usbHelper.usbSendData(SerialProtocol.A2_VERSION);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
